import { message, Modal } from "ant-design-vue";
import { Ref, createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { editStatusApi } from "@/api/configCenter/accountKeeper";
import { addtDeptAccountPageApi } from "@/api/configCenter/accountKeeper";
import FormHook from "@/hook/formHook";

export default function OperationHook(state: {
  formState: AccountKeeper.FormState;
  loading: Ref<boolean>;
  emit: {
    (e: "submit"): void;
    (e: "update:visible", visible: boolean): void;
  };
  formDataRef: Ref<any>;
  passData: Ref<AccountKeeper.ListRows>;
}) {
  const { formState, loading, emit, formDataRef, passData } = state;

  const { validatePhoneInputHandle } = FormHook({ formState });

  const onFish = () => {
    console.log("onFish");
  };

  //新增
  const onSubmit = () => {
    formDataRef.value.validate().then(() => {
      const params = {
        ...formState
      };

      loading.value = true;
      return addtDeptAccountPageApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };

  //编辑
  const editHandle = () => {
    formDataRef.value.validate().then(() => {
      const { bankName, sisterBankNum, isSyncBill, remark } = formState.accountList[0].deptAccountDtoList[0];

      loading.value = true;

      const params = {
        bankName,
        sisterBankNum,
        isSyncBill,
        remark,
        id: passData.value.id
      };

      editStatusApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };
  const resetForm = () => {
    formDataRef.value.resetFields();
    emit("update:visible", false);
  };

  const addAccount = () => {
    formState.accountList.push({
      accountProperties: "", //账户性质
      name: "", //户名
      deptAccountDtoList: [
        {
          shortName: "", //账户简称
          receiveAccount: "", //账号
          openBank: "", //开户银行
          bankName: "", //开户行名称
          currency: "", //币种
          sisterBankNum: "", //联行号
          paymentMethod: "", //收款方式
          isSyncBill: 0, //是否同步账单
          remark: "" //备注
        }
      ]
    });
  };
  const addChilren = (index: number) => {
    formState.accountList[index].deptAccountDtoList.push({
      shortName: "", //账户简称
      receiveAccount: "", //账号
      openBank: "", //开户银行
      bankName: "", //开户行名称
      currency: "", //币种
      sisterBankNum: "", //联行号
      paymentMethod: "", //收款方式
      isSyncBill: 0, //是否同步账单
      remark: "" //备注
    });
  };

  const deleteChilrenAccount = (index: number, innerIndex: number) => {
    Modal.confirm({
      title: "是否删除账号?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        formState.accountList[index].deptAccountDtoList.splice(innerIndex, 1);
      }
    });
  };

  const deleteAccount = (index: number) => {
    Modal.confirm({
      title: "是否删除账户?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        formState.accountList.splice(index, 1);
      }
    });
  };
  return {
    onFish,
    resetForm,
    onSubmit,
    addAccount,
    addChilren,
    deleteChilrenAccount,
    deleteAccount,
    editHandle,
    validatePhoneInputHandle
  };
}
